﻿using System;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Vendors;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Read.API.Applicantions.Command.ProcurementsCommand;
using T.E.ERP.Read.API.Applicantions.Command.VendorsCommand;
using T.E.ERP.Read.API.Dtos.Dtos;

namespace T.E.ERP.Read.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购订单查询分页
    /// </summary>
    public class PurchaseorderCommandHander : IRequestHandler<PurchaseorderCommand, APIResult<List<SupplierDto>>>
    {
        /// <summary>
        /// 采购订单
        /// </summary>
        private readonly IBaseRepository<Purchaseorder> baseRepository;
        /// <summary>
        /// 物料信息表
        /// </summary>
        private readonly IBaseRepository<Materials_Info> WaterbaseRepository;
        /// <summary>
        /// 异常处理
        /// </summary>
        private readonly ILogger<PurchaseorderCommandHander> logger;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        /// <param name="waterbaseRepository"></param>
        public PurchaseorderCommandHander(IBaseRepository<Purchaseorder> baseRepository, IBaseRepository<Materials_Info> waterbaseRepository, ILogger<PurchaseorderCommandHander> logger)
        {
            this.baseRepository = baseRepository;
            WaterbaseRepository = waterbaseRepository;
            this.logger = logger;
        }




        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<List<SupplierDto>>> Handle(PurchaseorderCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<SupplierDto>> result = new APIResult<List<SupplierDto>>();

            try
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();

                var list = from a in baseRepository.GetAll().Result
                           join b in WaterbaseRepository.GetAll().Result
                           on a.PurchaseOrderId equals b.MaterialsInfoid
                           select new SupplierDto
                           {
                               PurchaseOrderId = a.PurchaseOrderId,
                               Document = b.Document,
                               DocumentStatuss = b.DocumentStatus,
                               DocumentCode = a.DocumentCode,
                               PurchasingDe = a.PurchasingDe,
                               Supid = a.Supid,
                               MaterialName = b.MaterialName,
                               Materialid = b.Materialid,
                               ModeDate = b.ModeDate,
                               ModeQuantity = b.ModeQuantity,
                               ModeReceived = b.ModeReceived,
                               DocumentStatus = a.DocumentStatus,
                               BusinessStatus = a.BusinessStatus,
                           };

                if (!string.IsNullOrEmpty(request.Document))
                {
                    list = list.Where(x => x.Document == request.Document);
                }

                if (!string.IsNullOrEmpty(request.DocumentCode))
                {
                    list = list.Where(x => x.DocumentCode.Contains(request.DocumentCode));
                }

                if (!string.IsNullOrEmpty(request.Supid))
                {
                    list = list.Where(x => x.Supid.Contains(request.Supid));
                }

                if (!string.IsNullOrEmpty(request.MaterialName))
                {
                    list = list.Where(x => x.MaterialName.Contains(request.MaterialName));
                }

                // 添加需求日期范围查询条件
                if (request.ModeDateStart.HasValue && request.ModeDateEnd.HasValue)
                {
                    list = list.Where(x => x.ModeDate >= request.ModeDateStart && x.ModeDate <= request.ModeDateEnd);
                }
                else if (request.ModeDateStart.HasValue)
                {
                    list = list.Where(x => x.ModeDate >= request.ModeDateStart);
                }
                else if (request.ModeDateEnd.HasValue)
                {
                    list = list.Where(x => x.ModeDate <= request.ModeDateEnd);
                }

                result.data = list.ToList();
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "请求处理失败");
                throw;
            }
            return Task.FromResult(result);
        }
    }
}